package com.dmtt.web.service.impl;

import com.dmtt.web.common.ExportServiceImpl;
import com.dmtt.web.entity.ActivationVo;
import com.dmtt.web.entity.CarStatus;
import com.dmtt.web.entity.RealTimeData;
import com.dmtt.web.entity.TotalCount;
import com.dmtt.web.mapper.CarStatusMapper;
import com.dmtt.web.mapper.HomePageMapper;
import com.dmtt.web.service.HomePageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HomePageServiceImpl implements HomePageService {
    @Autowired
    private HomePageMapper homePageMapper;
    @Autowired
    private ExportServiceImpl exportService;
    @Autowired
    private CarStatusMapper carStatusMapper;

    //首页状态列表
    @Override
    public List<CarStatus> carStatusList(ActivationVo activationVo) {
        String agvName = activationVo.getAgvName();
        if (StringUtils.isNotEmpty(agvName)){
            agvName = "%" + activationVo.getAgvName() + "%";
        }
        List<CarStatus> carStatusList = homePageMapper.carStatusList(agvName);
        List<CarStatus> carStatuses11 = new ArrayList<>();
        for (CarStatus carStatus : carStatusList) {
            CarStatus carStatus2 = homePageMapper.currStatus(carStatus.getAgvName());
            CarStatus carStatus1 = new CarStatus();
            if (carStatus2 != null) {
                carStatus1 =  carStatus2;
                if (null != carStatus.getJobName()) {
                    carStatus1.setJobName(carStatus.getJobName());
                }
                if (null != carStatus.getElectric()) {
                    carStatus1.setElectric(carStatus.getElectric());
                }
                if (null != carStatus.getPoint()) {
                    carStatus1.setPoint(carStatus.getPoint());
                }
                if (null != carStatus.getGroupName()) {
                    carStatus1.setGroupName(carStatus.getGroupName());
                }
                List<CarStatus> carStatus3 = homePageMapper.grainStatistics();
                for (CarStatus status : carStatus3) {
                    if (status!=null) {
                        if (status.getAgvName().equals(carStatus.getAgvName())) {
                            carStatus1.setGrain(status.getGrain() + "%");
                        }
                    }
                }
                carStatuses11.add(carStatus1);
            }
        }
        if (null == activationVo.getAgvStatus() ){
            for (CarStatus carStatus : carStatuses11) {
                switch (carStatus.getAgvStatus()) {
                    case "调试":
                        carStatus.setAgvStatus("调试");
                        carStatus.setErrStatus("调试");
                        carStatus.setFlag(4);
                        break;
                    case "工作正常":
                    case "障碍物停车":
                    case "交通管制":
                    case "空车行驶":
                    case "装卸货等待":
                    case "单机":
                        carStatus.setAgvStatus("执行中");
                        carStatus.setErrStatus("执行中");
                        carStatus.setFlag(2);
                        break;
                    case "无任务等待":
                    case "关机":
                        carStatus.setErrStatus("待机");
                        carStatus.setAgvStatus("待机");
                        carStatus.setFlag(3);
                        break;
                    default:
                        carStatus.setErrStatus(carStatus.getAgvStatus());
                        carStatus.setAgvStatus("异常");
                        carStatus.setFlag(1);
                        break;
                }
            }
        } else {
            List<CarStatus> carStatuses = new ArrayList<>();
            for (CarStatus carStatus : carStatuses11) {
                if (carStatus.getAgvStatus().equals("调试") && activationVo.getAgvStatus().equals("3")){
                    carStatus.setFlag(4);
                    carStatus.setErrStatus(null);
                    carStatuses.add(carStatus);
                } else if ((carStatus.getAgvStatus().equals("工作正常") || carStatus.getAgvStatus().equals("障碍物停车") ||
                        carStatus.getAgvStatus().equals("交通管制") || carStatus.getAgvStatus().equals("空车行驶") ||
                        carStatus.getAgvStatus().equals("装卸货等待") || carStatus.getAgvStatus().equals("单机"))
                        && activationVo.getAgvStatus().equals("1") ){
                    carStatus.setFlag(2);
                    carStatus.setAgvStatus("执行中");
                    carStatus.setErrStatus(carStatus.getAgvStatus());
                    carStatuses.add(carStatus);
                } else if ((carStatus.getAgvStatus().equals("无任务等待") || carStatus.getAgvStatus().equals("关机"))  && activationVo.getAgvStatus().equals("2")){
                    carStatus.setFlag(3);
                    carStatus.setAgvStatus("待机");
                    carStatus.setErrStatus(carStatus.getAgvStatus());
                    carStatuses.add(carStatus);
                } else if ((!carStatus.getAgvStatus().equals("工作正常") && !carStatus.getAgvStatus().equals("障碍物停车") &&
                        !carStatus.getAgvStatus().equals("交通管制") && !carStatus.getAgvStatus().equals("空车行驶") &&
                        !carStatus.getAgvStatus().equals("装卸货等待") && !carStatus.getAgvStatus().equals("单机") &&
                        !carStatus.getAgvStatus().equals("调试") && !carStatus.getAgvStatus().equals("无任务等待") &&
                        !carStatus.getAgvStatus().equals("关机"))
                        && activationVo.getAgvStatus().equals("4")){
                    carStatus.setFlag(1);
                    carStatus.setAgvStatus("异常");
                    carStatus.setErrStatus(carStatus.getAgvStatus());
                    carStatuses.add(carStatus);
                }
            }
            carStatuses11.clear();
            carStatuses11.addAll(carStatuses);
        }
        Collections.sort(carStatuses11, new Comparator<CarStatus>() {
            @Override
            public int compare(CarStatus o1, CarStatus o2) {
                if(o1.getFlag() > o2.getFlag()){
                    return 1;
                }
                if(o1.getFlag() == o2.getFlag()){
                    return 0;
                }
                return -1;
            }
        });
        return carStatuses11;
    }

    //首页状态导出
    @Override
    public ResponseEntity<byte[]> carStatusListExport(ActivationVo activationVo) {
        try{
            List<CarStatus> movementRate = carStatusList(activationVo);
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            List<String> title = new ArrayList<>();
            title.add("车辆编号");
            title.add("车辆组id");
            title.add("任务名称");
            title.add("车辆状态");
            title.add("车辆异常状态");
            title.add("剩余电量");
            return exportService.buildResponseEntity(exportService.export((List<CarStatus>) movementRate,title,11),  formatter.format(calendar.getTime()) + ".xls");
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    //首页地图点击弹框
    @Override
    //@Transactional(rollbackFor = Exception.class)
    public List<CarStatus> realTimeData(ActivationVo activationVo){
        return carStatusList(activationVo);
    }

    @Override
    public TotalCount totalCount() {
        ActivationVo activationVo = new ActivationVo();
        List<CarStatus> carStatusList = carStatusList(activationVo);
        TotalCount totalCounts = new TotalCount();
        int work=0;
        int wait=0;
        int err=0;
        int debugging=0;
        for (CarStatus carStatus : carStatusList) {
            if (carStatus.getAgvStatus().equals("执行中")){
                ++work;
            } else if (carStatus.getAgvStatus().equals("待机")){
                ++wait;
            } else if (carStatus.getAgvStatus().equals("调试")){
                ++debugging;
            } else {
                ++err;
            }
        }
        totalCounts.setErr(err);
        totalCounts.setWait(wait);
        totalCounts.setDebugging(debugging);
        totalCounts.setWork(work);
        return totalCounts;
    }
}
